package com.zhz.lib.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.vo.custom.CustomVO;
import com.zhz.project.common.util.alibaba.StringUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午10:54:39
 * 
 */
@Controller
@RequestMapping("/custom.htm")
public class CustomController {
	private static Logger logger = Logger.getLogger(CustomController.class);
	// 查找资料
	private static String FIND = "find";
	// 查找资料
	private static String SERVICE = "service";
	// 查找资料
	private static String HELP = "help";
	// 查找资料
	private static String ABOUT = "about";

	@Autowired
	private CustomService customService;

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public String get(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		String idString = request.getParameter("id");
		CustomVO voCustomVO = customService.getCustomByCustomID(idString);
		if (voCustomVO != null) {
			CustomVO topCustomVO = null;
			CustomVO levelOneCustomVO = null;
			CustomVO levelTwoCustomVO = null;

			// 如果是顶级模块
			if (StringUtil.equals(voCustomVO.getLevel(), "0")) {
				topCustomVO = voCustomVO;
				levelOneCustomVO = customService.getFirstOfSubCustom(voCustomVO
						.getCustomId()
						+ "");
				levelTwoCustomVO = customService
						.getFirstOfSubCustom(levelOneCustomVO.getCustomId()
								+ "");
			}
			// 如果是一级模块
			else if (StringUtil.equals(voCustomVO.getLevel(), "1")) {
				levelOneCustomVO = voCustomVO;
				topCustomVO = customService.getCustomByCustomID(voCustomVO
						.getSortType());
				levelTwoCustomVO = customService.getFirstOfSubCustom(voCustomVO
						.getCustomId()
						+ "");
			}
			// 如果是二级模块
			else if (StringUtil.equals(voCustomVO.getLevel(), "2")) {
				levelTwoCustomVO = voCustomVO;
				levelOneCustomVO = customService.getCustomByCustomID(voCustomVO
						.getSortType());
				topCustomVO = customService
						.getCustomByCustomID(levelOneCustomVO.getSortType());
			}

			List<CustomVO> levelOneList = customService
					.getSubCustom(topCustomVO.getCustomId() + "");
			List<CustomVO> levelTwoList = customService
					.getSubCustom(levelOneCustomVO.getCustomId() + "");

			model.put("voCustomVO", voCustomVO);
			model.put("topCustomVO", topCustomVO);
			model.put("levelOneCustomVO", levelOneCustomVO);
			model.put("levelTwoCustomVO", levelTwoCustomVO);
			model.put("levelOneList", levelOneList);
			model.put("levelTwoList", levelTwoList);

			if (levelTwoList != null && levelTwoList.size() > 0)
				model.put("levelTwoListShow", "levelTwoListShow");
			customService.customHitPlusOne(Integer.parseInt(idString));

			if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(), FIND)) {
				return FIND;
			} else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(),
					SERVICE)) {
				return SERVICE;
			} else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(),
					HELP)) {
				return HELP;
			} else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(),
					ABOUT)) {
				return ABOUT;
			}
			return null;
		}
		return null;
	}
}
